/**
 * Created by Administrator on 2017/7/24.
 */
var newUserList = new Object();
var page;
var company_name;
var companyIds = [];
var deptIds = [];
var comPIds = [];
var userObject = [];
var userCompanyId = $("#userCompanyId").val(); // 当前登录用户所在的companyId

// 初始化
newUserList.init = function (page) {
    page = page;
    $("#recursion").attr("checked",false);
    window.groupTree_search = newUserList.search;
    window.groupTree_rightClick = newUserList.onRightClick;
    groupTree.init({"ifShow":"false"});
    newUserList.addDefaultNode();

    this.initTable();
};

newUserList.initTable = function () {
    var companyId = $("#companyId").val();
    $("#deptIds").val(companyId);
    var rootNode = getRoots()[0];
    newUserList.getAllchildrenNodes(comPIds, rootNode, rootNode.type);

    var selectedNodes = $.fn.zTree.getZTreeObj("group_tree").getSelectedNodes();
    var selectedNode = selectedNodes[0];
    newUserList.treeSearch(selectedNode);

};

newUserList.initListTable = function (url) {
    var list_url = url;
    var selectedNodes = $.fn.zTree.getZTreeObj("group_tree").getSelectedNodes();
    var selectedNode = selectedNodes[0];
    var cid = selectedNode.companyId;

    var roleType = $("input[name='roleType']:checked").val();
    if(roleType=='4'){
        $(".isShowRole").hide();
    }else {
        $(".isShowRole").show();
    }

    /*
     * 分页
     * @param param_form 参数form id
     * @param list_url 请求链接
     * @param tbody_id 列表div id
     * @param page_id 分页id
     * @param pageNum 页数
     * @param pageSize 每页条数
     */
    page.init("userListForm", list_url, "user_tbody", "user_page", 1, 10);

    //初始化
    page.goPage(1);

    //重写组装方法
    page.list = function (dataList) {
        var len = dataList.length;
        var inner = "", item;
        var nodeType = $("#nodeType").val();
        for (var i = 0; i < len; i++) {
            item = dataList[i];
            // 组装数据
            inner += "<tr>";
            inner += "<td width='40'>" + (parseInt(i) + 1) + "</td>";
            inner += "<td width='50' ><input type='checkbox' id='userId_" + i + "' title='" + item.userType + "' name='checkUser' value='" + item.id + "' companyId='" + item.companyId + "' userName='" + item.userName + "' /></td>";
            inner += "<td width='100'><span class=\"text-orange\">" + item.userName + "</span></td>";
            inner += "<td width='100'>" + item.userAccount + "</td>";
            if(roleType!='4'){
                inner += "<td width='80'><span class=\"text-orange\">" + item.roleName + "</span></td>";
            }
            /* var newCompanyName=getAllParentNodes (item.departmentId);departmentName
             if(newCompanyName == '' || newCompanyName==undefined || newCompanyName==null){
                 inner += "<td >"+item.companyName+"</td>";
             }else{
                 inner += "<td >"+newCompanyName+"</td>";
             }*/
            var company_name = item.companyName;
            if (company_name == '' || company_name == undefined || company_name == null) {
                inner += "<td width='100'>/</td>";
            } else {
                inner += "<td width='100'>" + company_name + "</td>";
            }
            var dept_name = item.departmentName;
            if(dept_name){
                dept_name = dept_name.trim();
            }
            if (dept_name == '' || dept_name == undefined || dept_name == null) {
                inner += "<td >/</td>";
            } else {
                inner += "<td ><span class=\"text-orange\">" + dept_name + "</span></td>";
            }
            inner += "<td width='60'>" + item.requirementYearStudyTime + "</td>";
            inner += "<td width='92'>" + TimeUtil.longMsTimeConvertToDateTime(item.registDate) + "</td>";
            inner += "<td width='200'><a href=\"" + appPath + "/admin/user/userInfo?id=" + item.id + "\" target=\"_blank\" class=\"a a-view\">详情</a>&nbsp;";
            if(cid == userCompanyId){
                var isable = item.isValid;
                var uType = item.userType;
                if ("1" == isable) {
                    inner += "<a href=javascript:modifyUser('" + item.id + "') target=\"_self\" class='a a-info'>修改</a>";
                    //inner += "<a href=\"" + appPath + "/admin/user/userModify?id=" + item.id + "\" target=\"_blank\" class='a a-info'>修改</a>";
                    inner += "<a href=javascript:restPassWord('" + item.id + "') class='a a-publish'>重置密码</a>";
                } else {
                    inner += "<a href=javascript:; class='a'>修改</a>";
                }
                var roleName = $("#roleName").val();
                if ("department_admin" == roleName && uType.indexOf('4') != -1) {
                    inner += "<a href=javascript:void(0) class='a'>删除</a>";
                } else {
                    inner += "<a href=javascript:deleteUser('" + item.id + "','"+ uType  +"') class='a a-close'>删除</a>";
                }
            }

            inner += "</td>";
            inner += "</tr>";
        }
        return inner;
    }

};

/**
 * 是否显示子单位节点
 * @param treeNode
 */
newUserList.checkboxChange = function () {
    if ($("#recursion").prop("checked")) {
        groupTree.init({"ifShow":"true"});
    }else{
        groupTree.init({"ifShow":"false"});
    }
    newUserList.addDefaultNode();
    newUserList.search();
};

newUserList.search = function (treeNode) {
    // 清空所有已选
    //$("#clearSelAll").trigger("click");
    var selectedNodes = $.fn.zTree.getZTreeObj("group_tree").getSelectedNodes();
    var selectedNode = selectedNodes[0];
    if("1" == selectedNode.type && $("#recursion").prop("checked") == true){
        $("#ifAllSearch").val("true");
    }else{
        $("#ifAllSearch").val("false");
    }
    newUserList.treeSearch(selectedNode);
};

//模糊查询
newUserList.treeSearch = function (treeNode) {
    // 获取左侧选中节点
    var companyId = (treeNode.companyId) ? treeNode.companyId : "";
    var departmentId = (treeNode.departmentId) ? treeNode.departmentId : "";
    var departmentName = (treeNode.name) ? treeNode.name : "";
    var type = treeNode.type;
    var id = treeNode.id.toString();
    $("#departmentId").attr("value", departmentId);
    $("#departmentName").attr("value", departmentName);
    $("#nodeType").val(type);
    var url = "";
    var array = [];
    newUserList.getAllchildrenNodes(array,treeNode, type);
    $("#companyId").val(companyId);
    var roleName = $("#roleName").val();
    //type 1:根节点(单位); 2:父节点(单位); 3:叶子节点(部门)
    if ("1" == type || "2" == type) {
        // 控制按钮
        if($("#admin").is(":checked")){
            $(".admin .addNo").removeClass("dis");
            $(".admin .addUser").addClass("dis");
            if("2" == type){
                $(".admin .delNo").removeClass("dis");
                $(".admin .delUser").addClass("dis");
            }else{
                $(".admin .delNo").addClass("dis");
                $(".admin .delUser").removeClass("dis");
            }
        }
        $("#companyIds").val(companyId);
        url = appPath + "/popup/userList";
    }
    if ("3" == type) {
        $("#companyId").val(companyId);
        $("#deptIds").val(array);
        url = appPath + "/popup/deptUserList";

        // 角色为单位管理员的不允许新增管理员
        if("department_admin" == $("#roleName").val() || userCompanyId != companyId){
            $(".admin .addNo").addClass("dis");
            $(".admin .addUser").removeClass("dis");
            if(userCompanyId != companyId){
                    $(".admin .delNo").removeClass("dis");
                    $(".admin .delUser").addClass("dis");
            }else{
                $(".admin .delNo").addClass("dis");
                $(".admin .delUser").removeClass("dis");
            }
        }else{
            $(".admin .addNo").addClass("dis");
            $(".admin .addUser").removeClass("dis");
            $(".admin .delNo").addClass("dis");
            $(".admin .delUser").removeClass("dis");
        }
    }
    if('-1' == departmentId){
        $(".admin .addNo").removeClass("dis");
        $(".admin .addUser").addClass("dis");
    }
    //非本单位
    if(companyId != userCompanyId){
        if("department_admin" != $("#roleName").val()){
            $(".student .btnHidden").show();
            $(".student .btnShow").hide();
        }

    }else{
        if("department_admin" != $("#roleName").val()){
            $(".student .btnHidden").hide();
            $(".student .btnShow").show();
        }
    }
    newUserList.initListTable(url);
};


/**
 * 递归查询人员所在单位
 * @param deptId
 * @returns {string}
 */
function getAllParentNodes(deptId) {
    var contactName = '';
    var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
    var treeNode = treeObj.getNodeByParam("id", deptId, null);
    if (treeNode == null) {
        return "";
    } else if (treeNode.type == "1") {
        return "";
    } else {
        contactName = contactName == '' ? treeNode.name : treeNode.name + '/' + contactName;
        var vl = getAllParentNodes(treeNode.getParentNode().id);
        if (vl != '') {
            contactName = contactName == '' ? vl : vl + '/' + contactName;
        }
    }
    return contactName;
}

//获取某一节点下的所有子节点
newUserList.getAllchildrenNodes = function (array, treeNode, type) {
    // if("-1" != treeNode.id) {
        array.push(treeNode.id);
   //  }
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            var len = childrenNodes.length;
            if (type == '3') {
                for (var i = 0; i < len; i++) {
                    newUserList.getAllchildrenNodes(array, childrenNodes[i], type);
                }
            } else {
                for (var j = 0; j < len; j++) {
                    if (childrenNodes[j].type == '3') {
                        continue;
                    }
                    newUserList.getAllchildrenNodes(array,childrenNodes[j], type);
                }
            }
        }
    }
    return array;
};

//查询全部
newUserList.searchAll = function () {
    $("#userName").val("");
    $("#userAccount").val("");
    $("#register_start_time").val("");
    $("#register_end_time").val("");
    // 清空默认选择项
    $("#role_select2").val("").trigger("change");
    $("#trainRoleId").val("");
    newUserList.search();
};


//搜索高亮
function getFontCss(treeId, treeNode) {
    return (!!treeNode.highlight) ? {'color': "#A60000", 'font-weight': "bold"} : {
        'color': "#333",
        'font-weight': "normal"
    };
}

// 获取根节点集合
function getRoots() {
    var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
    return treeObj.getNodesByFilter(function (node) {
        return node.level == 0;
    });
}


newUserList.getRole = function () {
    var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
    var rootNode = this.getRoots()[0];
    treeObj.selectNode(rootNode);
    treeObj.setting.callback.onClick(null, tree_options.id, rootNode);
};


/**
 * 导入
 */
newUserList.importUser = function () {
    // 获取部门id
    var departmentId = "";
    var companyId = "";
    var nodeId = "";
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length != 0) {
        companyId = selectedNodes[0].companyId;
        nodeId = selectedNodes[0].id;
        if (selectedNodes[0].departmentId) {
            departmentId = selectedNodes[0].departmentId;
        }
    }
    if(nodeId != "" && companyId == nodeId){
        layer.msg("单位节点不支持人员导入");
        return;
    }
    if(!commonMsg(departmentId,"不支持人员导入")){
        return;
    };
    // 导入模板类型 1：学员模板  2：角色模板
    //exportTye 1调用学员导出方法 2调用角色导出方法
    var tempType = "1";
    var params = new Object();
    params.departmentId = departmentId;
    params.companyId = companyId;
    layer.open({
        type: 2,
        title: '导入excel',
        shadeClose: false,				//true点击遮罩层关闭
        shade: 0.3,					//遮罩层背景
        maxmin: true, 					// 开启最大化最小化按钮
        area: ['1000px', '80%'],		//弹出层大小
        scrollbar: false,				//false隐藏滑动块
        content: [appPath + '/admin/excelImport/to_import?tempType=' + tempType + '&exportTye=1' + '&params=' + encodeURIComponent(JSON.stringify(params)), 'yes'],
        cancel: function () {
            //刷新数据
            returnUrl();
        }
    })
};

//全选与非全选
/*$("#checkAll").click(function () {
    if (this.checked) {
        $("#user_tbody :checkbox").prop("checked", true);
    } else {
        $("#user_tbody :checkbox").prop("checked", false);
    }
});*/
/**
 * checkbox 点击事件
 * @param selectedNode
 */
var userIdAll = [];
var object = {};

/**
 * 全选事件
 */
$("#checkAll").click(function () {
        if (this.checked) {
            $("#user_tbody :checkbox").prop("checked", true);

            $("#user_tbody :checkbox").each(function () {
                if (!object[$(this).val()]) {
                    var item = new Object();
                    item.userId = $(this).val();
                    item.companyId = $(this).attr("companyid");
                    item.userType = $(this).attr("title");
                    item.userName = $(this).attr("username");
                    object[$(this).val()] = 1;
                    userIdAll.push(item);
                }else{
                    if(userIdAll.indexOf($(this).val()) == -1){
                        var item = new Object();
                        item.userId = $(this).val();
                        item.companyId = $(this).attr("companyid");
                        item.userType = $(this).attr("title");
                        item.userName = $(this).attr("username");
                        userIdAll.push(item);
                    }
                }
            });

        } else {
            $("#user_tbody :checkbox").prop("checked", false);
            if (userIdAll.length > 0) {
                for (var a = 0; a < userIdAll.length; a++) {
                    $("#user_tbody :checkbox").each(function () {
                        if ($(this).val() === userIdAll[a].userId) {
                            delete object[$(this).val()];
                            userIdAll.splice(a, 1);
                        }
                    })
                }
            }
        }
    //console.log("11:"+JSON.stringify(userIdAll)+"//"+JSON.stringify(object));
    }

);

/**
 * 单选事件
 */
$("#user_tbody").on("click", "input[type=checkbox]", function () {
    if ($(this).is(":checked")) {
        if (!object[$(this).val()]) {
            object[$(this).val()] = 1;
            var item = new Object();
            item.userId = $(this).val();
            item.companyId = $(this).attr("companyid");
            item.userType = $(this).attr("title");
            item.userName = $(this).attr("username");
            userIdAll.push(item);
        }
    } else {
        if (userIdAll.length > 0) {
            for (var a = 0; a < userIdAll.length; a++) {
                if ($(this).val() === userIdAll[a].userId) {
                    delete object[$(this).val()];
                    userIdAll.splice(a, 1);
                }
            }
        }
    }
    //console.log("11:"+JSON.stringify(userIdAll)+"//"+JSON.stringify(object));
});
//console.log(userIdAll);
/**
 *
 * 清空所有的选择
 */
$("#clearSelAll").on("click",function(){
    userIdAll = [];
    object={};
});

newUserList.clearSelAll=function(){
    $("#clearSelAll").trigger("click");
    userIdAll = [];
    object={};
    // page.refresh_cur_page();
}

/*userList.exportUser = function(){
 if(page.count != 0){
 userList.to_exportUser();
 }else{
 layer.msg("没有可以导出的数据！");
 }
 };*/

/**
 * 导出
 */
newUserList.exportUser = function () {
    // 获取部门id
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    var treeNode = selectedNodes[0];
    var companyId = (treeNode.companyId) ? treeNode.companyId : "";
    var depId = (treeNode.departmentId) ? treeNode.departmentId : "";
    var departmentName = (treeNode.name) ? treeNode.name : "";
    var type = treeNode.type;
    var array = new Array;
   // var pkidObj = document.getElementsByName("checkUser");
   // for (var j = 0; j < pkidObj.length; j++) {
   //     if (pkidObj.item(j).checked == true) {
    //        array.push(pkidObj.item(j).value);
   //     }
   // }


    if(userIdAll.length>0){
        for(var j = 0; j < userIdAll.length; j++){
            array.push(userIdAll[j].userId);
        }
    }
    var arr = array.join(",");
    var fileName = departmentName + "-人员导出.xlsx";

    var flag = $("#recursion").prop("checked");

    companyIds = [];
    if (null != arr && '' != arr) {
        if(array.length>500){
            layer.msg("您的选择大于500条！");
            return;
        }
        window.location.href = appPath + '/admin/excelExport/export?fileName=' + fileName + '&params=' + arr + "&exportTye=1" + "&flag=" + flag;
    } else if ("3" == type) {  //导出部门人员
        if (flag && '-1' == depId) {
            companyIds.push(comPIds);
        } else {
            companyIds.push(companyId);
        }
        var pageTotal = Number($("#pageTotal").val());
        if(pageTotal && !isNaN(pageTotal)){
            if(pageTotal>500){
                layer.msg("数据量大于500,请选择小于500条！");
                return;
            }
        }else{
            return;
        }
        //询问框
        layer.confirm('您想要导出' + '<span style="color: blue;font-weight: bold">' + departmentName + '</span>' + '部门下的人员？', {
            btn: ['确定', '取消'], //按钮
            icon: 3
        }, function (index) {
            window.location.href = appPath + '/admin/excelExport/export?fileName=' + fileName + '&depId=' + depId + '&companyIds=' + companyIds + "&exportTye=1" + "&flag=" + flag;
            layer.close(index);
        }, function () {
            //取消
        });
    } else {
        var pageTotal = Number($("#pageTotal").val());
        if(pageTotal && !isNaN(pageTotal)){
            if(pageTotal>500){
                layer.msg("数据量大于500,请选择小于500条！");
                return;
            }
        }else{
            return;
        }
        if (flag) {
            companyIds = newUserList.getAllchildrenNodes(companyIds, treeNode, type);
        } else {
            companyIds.push(companyId);
        }


        //询问框
        layer.confirm('您想要导出全公司人员？', {
            btn: ['确定', '取消'], //按钮
            icon: 3
        }, function (index) {
            window.location.href = appPath + '/admin/excelExport/export?fileName=' + fileName + "&exportTye=1" + "&companyIds=" + companyIds + "&flag=" + flag + "&type=" + type;
            layer.close(index);
        }, function () {
            //取消
        });
    }
};

/**
 * 转移
 */
newUserList.moveUser = function () {
    userObject = userIdAll;
  //  userObject = array;
    if (userIdAll.length == 0) {
        layer.msg("未勾选人员");
        return;
    }

    var userIds = encodeURI(JSON.stringify(userIdAll));
    // 弹窗-选择部门
    layer.open({
        type: 2,
        title: '选择部门',
        shadeClose: false,				//true点击遮罩层关闭
        shade: 0.3,					//遮罩层背景
        maxmin: true, 					// 开启最大化最小化按钮
        area: ['  px', '60%'],		//弹出层大小
        scrollbar: false,				//false隐藏滑动块
        content: [appPath + '/admin/user/selectDept?companyId=' + $("#companyId").val(), 'yes']
    });
   $("#clearSelAll").trigger("click");
};


// 人员离职
newUserList.quitUser = function () {
    // 获取部门id
    var departmentId = "";
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length != 0) {
        if (selectedNodes[0].departmentId) {
            departmentId = selectedNodes[0].departmentId;
        }
    }
    if(!commonMsg(departmentId,"不支持人员新增")){
        return;
    };
    // 获取选择人员
    var array = new Array;
    if(userIdAll.length>0){
        for(var j = 0; j < userIdAll.length; j++){
            array.push(userIdAll[j].userId);
            if(userCompanyId != userIdAll[j].companyId){
                layer.msg("不允许离职其他公司人员");
                return;
            }
        }
    }
    if (userIdAll.length == 0) {
        layer.msg("未勾选人员");
        return;
    }
    layer.confirm("确定执行此操作?", {
        icon: 3,
        btn: ["确认", "取消"]
    }, function () {
        $.ajax({
            url: appPath + '/admin/user/changeUserQuit',
            async: false,
            type: 'post',
            data: {
                'departmentId': "-1",
                'uids': array.join(',')
            },
            success: function (data) {
                var result = data.result;
                if (result) {
                    layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon',closeBtn:0}, function (index) {
                        layer.close(index);

                        // 刷新
                        newUserList.search();
                        //清楚权限按钮
                        $("#all",window.document).prop("checked",false);
                    });
                } else {
                    layer.alert('操作失敗', {icon: 2, skin: 'layer-ext-moon',closeBtn:0});
                }
            }
        });
    });
};

/**
 * 批量删除
 */
newUserList.batchDelUser = function () {
    var selectUserType = $("#userType").val();
    // 获取选择人员

    var array = new Array;
    if(userIdAll.length>0){
        for(var j = 0; j < userIdAll.length; j++){
            var userType = userIdAll[j].userType + "";
            /*if(userType.indexOf("1") !== -1 || userType.indexOf("2") !== -1){
                layer.msg("勾选的人员中" + userIdAll[j].userName + "是管理员或监管员，请重新选择");
                return;
            }*/
            array.push(userIdAll[j].userId);
        }
    }

    if (array.length == 0) {
        layer.msg("未勾选人员");
        return;
    }

    // 管理员列表中删除人员时不需要校验学时，因为删除仅仅删除管理员聚角色
    if("4" != $("#roleType label input:checked").val()){
        if (checkHours(array.join(','))) {
            layer.msg("所选学员中有学员存在学时数据，请选择没有学时的学员");
            return;
        }
    }

    layer.confirm("确定删除吗?", {
        icon: 3,
        btn: ["确认", "取消"]
    }, function () {

        $.ajax({
            url: appPath + '/admin/user/batchDelUser',
            async: false,
            type: 'post',
            data: {'userIds': array.join(','),'userType':selectUserType},
            success: function (data) {
                var code = data['code'];
                var message;
                if (code == '10000') {
                    layer.alert('批量删除成功', {icon: 1, skin: 'layer-ext-moon',closeBtn:0}, function (index) {
                        layer.close(index);
                        // 刷新
                        //returnUrl();
                        userIdAll=[];
                        page.refresh_cur_page();
                    });
                } else {
                    layer.alert((data['message']) ? data['message'] : '批量删除失敗', {icon: 2, skin: 'layer-ext-moon',closeBtn:0});
                }
            }
        });

    });
};

function modifyUser(userId) {
    var url = appPath + "/admin/user/userModify?id=" + userId ;
    window.open(url, "_self");
}

/**
 * 删除
 */
function deleteUser(userId,userType) {
    var selectUserType = $("#userType").val();
    if (userId == null || userId == '') {
        layer.alert('删除失败', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    // 管理员列表中删除人员时不需要校验学时，因为删除仅仅删除管理员聚角色
    if("4" != $("#roleType label input:checked").val()){
        if (checkHours(userId)) {
            layer.msg("该学员已经有学时数据存在，不能删除");
            return;
        }
    }
    layer.confirm("确定删除吗?", {
        icon: 3,
        btn: ["确认", "取消"]
    }, function () {
        $.ajax({
            url: appPath + '/admin/user/delUser',
            async: false,
            type: 'post',
            data: {
                'userId': userId,	// 人员id
                'userType':userType,
                'selectUserType':selectUserType
            },
            success: function (data) {
                var code = data['code'];
                if (code == '10000') {
                    layer.msg('删除成功');
                    // 刷新
                    // returnUrl();
                    page.refresh_cur_page();
                } else {
                    layer.alert((data['message']) ? data['message'] : '删除失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    })
}

//查询是否有学时
function checkHours(userId) {
    var flag = false;
    $.ajax({
        url: appPath + '/admin/user/checkHours',
        async: false,
        type: 'post',
        data: {'userId': userId},
        success: function (data) {
            var result = data['result'];
            flag = result;
        }
    });
    return flag;
}

function returnUrl() {
    newUserList.search();
}

//详情
function userInfo(userId) {
    location.href = appPath + '/admin/user/userInfo?id=' + userId;
}

//密码重置
function restPassWord(userId) {
    layer.confirm("确定重置密码?", {
        icon: 3,
        btn: ["确定", "取消"]
    }, function () {
        // 用户重置密码
        newUserList.user_pwd_reset(userId);
    }, function () {
        // 取消
    });
}

// 用户重置密码
newUserList.user_pwd_reset = function (userId) {
    $.ajax({
        url: appPath + '/admin/user/resetPassWord',
        async: false,
        type: 'post',
        data: {
            'userId': userId	// 人员id
        },
        success: function (data) {
            var code = data.code;
            if (code == '10000') {
                var pwd_new = data.result;
                layer.alert('密码重置成功,学员密码为' + pwd_new, {icon: 1, skin: 'layer-ext-moon'});
            } else {
                layer.alert('操作失敗', {icon: 2, skin: 'layer-ext-moon'});
            }
        }
    });
};

newUserList.addDefaultNode = function () {
    var rootNode = getRoots()[0];
    var treeNode1 = new Object();
    treeNode1.id = '-1';
    treeNode1.pid = rootNode.id;
    treeNode1.name = '离职人员';
    treeNode1.companyId = rootNode.id;
    treeNode1.departmentId = "-1";
    treeNode1.type = '3';
    treeNode1.icon = appPath + "/static/global/js/zTree/css/zTreeStyle/img/department.png";
    groupTree.zTree.addNodes(rootNode, treeNode1, false);
};

//修改
function modifyPassWord(userId) {
    location.href = appPath + '/admin/user/userModify?id=' + userId;
}

//删除节点
newUserList.removeTreeNode = function (e) {
    hideRMenu();
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    var params = {
        'companyId': selectedNode.companyId,
        'departmentId': selectedNode.departmentId
    };
    if (selectedNode.children && selectedNode.children.length > 0) {
        layer.confirm('提示：您要删除的节点包含子节点，如果删除将连同子节点一起删掉', {
            btn: ['删除', '取消'] //按钮
        }, function () {
            newUserList.remove(params, selectedNode);
        }, function () {
            // 取消删除
        });
    } else {
        layer.confirm('确定删除吗？', {
            btn: ['删除', '取消'] //按钮
        }, function () {
            newUserList.remove(params, selectedNode);
        });
    }
};

// 删除节点
newUserList.remove = function (params, selectedNode) {
    $.ajax({
        url: appPath + '/admin/department/delDepartment',
        async: false,
        type: 'post',
        data: params,
        success: function (data) {
            var result = data['result'];
            var message;
            if (result) {
                layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon'}, function (index) {
                    layer.close(index);
                    // 刷新树
                    if ($("#recursion").prop("checked")) {
                        groupTree.init({"ifShow":"true"});
                    }else{
                        groupTree.init({"ifShow":"false"});
                    }
                    //展开当前节点以下的所有子节点
                    groupTree.expandNode(selectedNode.pId);
                    newUserList.addDefaultNode();
                });
            } else {
                layer.alert("操作失敗,部门下存在学员不能删除", {icon: 2, skin: 'layer-ext-moon'});
            }
        }
    });
};

//修改名称
newUserList.renameTreeNode = function (e) {
    hideRMenu();
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    var node = '';
    if (selectedNodes.length > 0) {
        node = selectedNodes[0].getParentNode();
    }
    // 请输入节点名称
    var promptIndex = layer.prompt({
        title: '输入新的节点名称，并确认',
        formType: 2,
        maxlength: 40,
        value: selectedNode.name
    }, function (text) {
        layer.close(promptIndex);
        var selectedNode = selectedNodes[0];
        var params = {
            'companyId': selectedNode.companyId,
            'departmentId': selectedNode.departmentId,
            'departmentName': $.trim(text),
            'departmentNames': node.pId == null ? '' : node.name
        };
        if (checkName($.trim(text), selectedNode.companyId, selectedNode.id, 'edit')) {
            layer.msg("部门名称已存在");
            return;
        }
        $.ajax({
            url: appPath + '/admin/department/updateDepartment',
            async: false,
            type: 'post',
            data: params,
            success: function (data) {
                var result = data['result'];
                var message;
                if (result) {
                    layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon'}, function (index) {
                        layer.close(index);

                        // 获取左侧选中节点
                        var companyId = (selectedNode.companyId) ? selectedNode.companyId : "";
                        var departmentId = (selectedNode.departmentId) ? selectedNode.departmentId : "";
                        var name = selectedNode.name;
                        $("#departmentId").attr("value", departmentId);
                        $("#companyId").attr("value", companyId);

                        newUserList.treeSearch(selectedNode);
                        //userList.initListTable();
                        // 刷新树
                        if ($("#recursion").prop("checked")) {
                            groupTree.init({"ifShow":"true"});
                        }else{
                            groupTree.init({"ifShow":"false"});
                        }
                        //展开当前节点以下的所有子节点
                        groupTree.expandNode(node.id);
                        newUserList.addDefaultNode();
                        /*selectedNode.name = value;
                        alert(name)
                        selectedNode.checked = true;
                        $.fn.zTree.getZTreeObj(tree_options.id).updateNode(selectedNode);*/
                    });
                } else {
                    layer.alert((data['message']) ? data['message'] : '操作失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    });
};

//新增单位
newUserList.addCompany = function (e) {
    hideRMenu();
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    var url = $("#createDeptUrl").val();
    var companyId = selectedNode.id;
    window.open(url + "/" + companyId);
    //location.href = appPath +"/admin/user/to_redirect?companyId="+companyId;
};

//新增部门
newUserList.addDept = function (e) {
    hideRMenu();
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    // 请输入节点名称
    var promptIndex = layer.prompt({
        title: '输入要添加的节点名称，并确认',
        formType: 2,
        maxlength: 40
    }, function (text) {
        var params = {
            'companyId': selectedNode.companyId,
            'departmentId': selectedNode.departmentId,
            'departmentName': $.trim(text)
        };
        if (text.length > 40) {
            layer.msg("部门名称超出长度限制");
            return;
        }
        if (checkName($.trim(text), selectedNode.companyId, selectedNode.id, null)) {
            layer.msg("部门名称已存在");
            return;
        }
        $.ajax({
            url: appPath + '/admin/department/addDepartment',
            async: false,
            type: 'post',
            data: params,
            success: function (data) {
                var result = data['result'];
                var message;
                if (result) {
                    layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon'}, function (index) {
                        layer.close(index);
                        layer.close(promptIndex);
                        // 刷新树
                        if ($("#recursion").prop("checked")) {
                            groupTree.init({"ifShow":"true"});
                        }else{
                            groupTree.init({"ifShow":"false"});
                        }
                        //展开当前节点以下的所有子节点
                        groupTree.expandNode(selectedNode.id);
                        newUserList.addDefaultNode();
                    });
                } else {
                    layer.alert((data['message']) ? data['message'] : '操作失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    });
};

// 新增管理员
newUserList.createUser = function () {
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    if (selectedNode.type == '1' || selectedNode.type == '2') {
        var url = $("#createUserUrl").val();
        var companyId = selectedNode.id;
        window.open(url + "/" + companyId);
    }

};

// 修改单位
newUserList.updataDeptUrl = function () {
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    if (selectedNode.type == '1' || selectedNode.type == '2') {
        var url = $("#updateDeptUrl").val();
        var companyId = selectedNode.id;
        window.open(url + "/" + companyId);
    }

};

function checkName(departmentName, companyId, parentId, type) {
    var flag = false;
    $.ajax({
        url: appPath + '/admin/department/checkDepartmentName',
        async: false,
        type: 'post',
        data: {
            'departmentName': departmentName,
            'companyId': companyId,
            'parentId': parentId,
            'operType': type
        },
        success: function (data) {
            var result = data['result'];
            flag = result;
        }
    });
    return flag;
}

newUserList.onRightClick = function (event, treeId, treeNode) {
    var scrollTop = $(window).scrollTop();
    var pageX = event.clientX;
    var pageY = event.clientY + scrollTop;
    zTree = $.fn.zTree.getZTreeObj(tree_options.id);
    zTree.selectNode(treeNode);
    showRMenu(pageX, pageY, treeNode);
};

// 人员新增
newUserList.addUser = function () {
    // 获取部门id
    var departmentId = "";
    var departmentName = "";
    var companyId = "";
    var nodeType = "";
    var nodeId = "";
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length != 0) {
        companyId = selectedNodes[0].companyId;
        nodeId = selectedNodes[0].id;
        nodeType = selectedNodes[0].type;
        if (selectedNodes[0].departmentId) {
            departmentId = selectedNodes[0].departmentId;
            departmentName = selectedNodes[0].name;
        }

    }
    if(nodeId != "" && companyId == nodeId){
        layer.msg("单位节点不支持人员新增");
        return;
    }
    if(!commonMsg(departmentId,"不支持人员新增")){
        return;
    };
    if(!userLimitCheck(companyId)){
        return;
    };
    layer.open({
        type: 2,
        title: '新增人员',
        area: ['900px', '730px'],		//弹出层大小
        scrollbar: false,				//false隐藏滑动块
        content: [appPath + '/addUser?departmentId=' + departmentId + "&companyId=" + companyId + "&departmentName=" + departmentName + "&nodeType=" + nodeType + "&userType=" + $("#userType").val(), 'yes']
    });
};

//批量修改
newUserList.batchModify = function () {
    var array = new Array;
    if(userIdAll.length>0){
        for(var j = 0; j < userIdAll.length; j++){
            array.push(userIdAll[j].userId);
        }
    }else{
        layer.msg("请至少选择一个人员");
        return;
    }

    var userIds = array.join(",");
    layer.open({
        type: 2,
        title: '批量修改',
        area: ['450px', '770px'],		//弹出层大小
        scrollbar: false,				//false隐藏滑动块
        content: [appPath +'/batchModify?userIds='+userIds, 'yes']
    });
};

function showRMenu(x, y, treeNode) {
    if ("1" == treeNode.type || "2" == treeNode.type) {//单位节点
        $("#m_add_company").hide();
        $("#m_update_dept").hide();
        $("#m_create_user").hide();
        $("#m_add_dept").show();
        $("#m_del").hide();
        $("#m_reset").hide();
        $("#m_moveup").hide();
        $("#m_movedown").hide();
        if(treeNode.companyId != userCompanyId || treeNode.departmentId == "-1"){
            $("#m_add_dept").hide();
        }
    } else {
        // 部门节点
        $("#m_add_dept").show();
        $("#m_del").show();
        $("#m_reset").show();
        $("#m_moveup").show();
        $("#m_movedown").show();
        $("#m_create_user").hide();
        $("#m_update_dept").hide();
        $("#m_add_company").hide();
        if(treeNode.companyId != userCompanyId || treeNode.departmentId == "-1"){
            $("#m_add_dept").hide();
            $("#m_del").hide();
            $("#m_reset").hide();
            $("#m_moveup").hide();
            $("#m_movedown").hide();
        }
    }
    $("#rMenu").css({"top": y + "px", "left": x + "px", "visibility": "visible"});

    $("body").bind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        rMenu.css({"visibility": "hidden"});
    }
}

function hideRMenu() {
    if (rMenu) rMenu.css({"visibility": "hidden"});
    $("body").unbind("mousedown", onBodyMouseDown);
}


// 上移
newUserList.moveUp = function () {
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    var params = {
        'companyId': selectedNode.companyId,
        'departmentId': selectedNode.departmentId
    };
    $.ajax({
        url: appPath + '/admin/department/moveUp',
        async: false,
        type: 'post',
        data: params,
        success: function (data) {
            var result = data['result'];
            var message;
            if (result) {
                layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon'}, function (index) {
                    layer.close(index);
                    // 刷新树
                    if ($("#recursion").prop("checked")) {
                        groupTree.init({"ifShow":"true"});
                    }else{
                        groupTree.init({"ifShow":"false"});
                    }
                    newUserList.addDefaultNode();
                });
            } else {
                layer.alert((data['message']) ? data['message'] : '操作失敗', {icon: 2, skin: 'layer-ext-moon'});
            }
        }
    });
};

// 下移
newUserList.moveDown = function () {
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length == 0) {
        layer.alert('请选择节点', {icon: 2, skin: 'layer-ext-moon'});
        return;
    }
    var selectedNode = selectedNodes[0];
    var params = {
        'companyId': selectedNode.companyId,
        'departmentId': selectedNode.departmentId
    };
    $.ajax({
        url: appPath + '/admin/department/moveDown',
        async: false,
        type: 'post',
        data: params,
        success: function (data) {
            var result = data['result'];
            var message;
            if (result) {
                layer.alert('操作成功', {icon: 1, skin: 'layer-ext-moon'}, function (index) {
                    layer.close(index);
                    // 刷新树
                    if ($("#recursion").prop("checked")) {
                        groupTree.init({"ifShow":"true"});
                    }else{
                        groupTree.init({"ifShow":"false"});
                    }
                    newUserList.addDefaultNode();
                });
            } else {
                layer.alert((data['message']) ? data['message'] : '操作失敗', {icon: 2, skin: 'layer-ext-moon'});
            }
        }
    });
};


newUserList.idCardEntry = function () {
    // 获取部门id
    var departmentId = "";
    var departmentName = "";
    var companyId = "";
    var nodeId = "";
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length != 0) {
        nodeId = selectedNodes[0].id;
        companyId = selectedNodes[0].companyId;
        if (selectedNodes[0].departmentId) {
            departmentId = selectedNodes[0].departmentId;
            departmentName = selectedNodes[0].name;
        }
    }
    if(nodeId != "" && companyId == nodeId){
        layer.msg("单位节点不支持人员录入");
        return;
    }
    if(!commonMsg(departmentId,"不支持录入")){
        return;
    };
    var param = {'companyId':companyId, 'departmentId':departmentId, 'departmentName': departmentName};

    location.href = appPath + "/admin/user/idCardEntry?companyId=" + companyId + "&departmentId=" + departmentId + "&departmentName=" + departmentName;

};

function commonMsg(id,text){
    if (id == "-1") {
        layer.msg("离职人员" + text);
        return false;
    }
    if (id == "-2") {
        layer.msg("调用人员" + text);
        return false;
    }
    return true;
}

// 校验该单位人员是否有设置上限
function userLimitCheck(companyId){
    var flag = true;
    $.ajax({
        url: appPath + '/admin/user/userLimitCheck',
        async: false,
        type: 'post',
        data: {"companyId":companyId},
        success: function (data) {
            var result = data['result'];
            //result: true表示已经到达上限，false表示未达到上限
            if (result ) {
                flag = false;
                layer.msg("有效学员数量已超过限值, 无法新增！")
            }
        }
    });
    return flag;
}

newUserList.selectUserListByUserType = function (value) {
    console.log(value)
}




/**
 * 导入人员证书
 */
newUserList.importCertificate = function () {
    // 获取部门id
    var departmentId = "";
    var companyId = "";
    var nodeId = "";
    var selectedNodes = $.fn.zTree.getZTreeObj(tree_options.id).getSelectedNodes();
    if (selectedNodes.length != 0) {
        companyId = selectedNodes[0].companyId;
        nodeId = selectedNodes[0].id;
        if (selectedNodes[0].departmentId) {
            departmentId = selectedNodes[0].departmentId;
        }
    }

    if(!commonMsg(departmentId,"不支持人员证书导入")){
        return;
    };

    if("" == companyId){
        layer.msg("单位id不能为空");
        return;
    }

    // 导入模板类型 1：学员模板  2：角色模板 6:证书导入
    //exportTye 1调用学员导出方法 2调用角色导出方法
    var tempType = "6";
    var params = new Object();
    if(companyId == departmentId){
        departmentId = "";
    }
    params.departmentId = departmentId;
    params.companyId = companyId;
    layer.open({
        type: 2,
        title: '导入文档',
        shadeClose: false,				//true点击遮罩层关闭
        shade: 0.3,					//遮罩层背景
        maxmin: true, 					// 开启最大化最小化按钮
        area: ['1000px', '80%'],		//弹出层大小
        scrollbar: false,				//false隐藏滑动块
        content: [appPath + '/admin/excelImport/to_import?tempType=' + tempType + '&exportTye=6'  + '&params=' + encodeURIComponent(JSON.stringify(params)), 'yes'],
        cancel: function () {
            //刷新数据
            returnUrl();
        }
    })
};


